package com.vf.editor.mvc.scene.model;

import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.vf.editor.common.controller.BaseController;
import com.vf.editor.common.model.resources.Model;
import com.vf.editor.common.model.resources.ModelClassify;
import com.vf.editor.common.model.resources.Scene;
import com.vf.editor.common.model.resources.SceneLayer;
import com.vf.editor.common.model.resources.SceneModel;
import com.vf.editor.common.util.UuidUtil;

@Path(value = "/scene/model", viewPath = "/views/scene/model/")
public class ModelController extends BaseController{

	@Inject
	private ModelService modelService;
	
	public void index() {
		Scene scene=Scene.dao.findById(getPara("sceneId"));
		this.set("scene", scene);
		render("index.html");
	}
	
	public void edit() {
		SceneModel sceneModel = SceneModel.dao.findById(getPara("id"));
		this.setAttr("sceneModel", sceneModel);
		render("edit.html");
	}
	
	public void edit3dtitle() {
		render("edit3dtitle.html");
	}
	
	public void libraryForModel() {
		List<ModelClassify> classifies=ModelClassify.dao.find("SELECT a.*,(select COUNT(id) count from src_model where classifyId=a.id and type <> '1' ) count FROM "+ModelClassify.TABLE_NAME+" a ORDER BY SORT ASC ");
		this.set("classifies", classifies);
		this.render("libraryForModel.html");
	}
	
	public void libraryFor3dtitle() {
		List<ModelClassify> classifies=ModelClassify.dao.find("SELECT a.*,(select COUNT(id) count from src_model where classifyId=a.id and type ='1' ) count FROM "+ModelClassify.TABLE_NAME+" a ORDER BY SORT ASC ");
		this.set("classifies", classifies);
		this.render("libraryFor3dtitle.html");
	}
	
	public void listDataForModel() {
		List<Model> list = Model.dao.find("select * from "+Model.TABLE_NAME+" where classifyId=? and type <> '1' ",this.getPara("classifyId"));
		renderPage(0, list, "", list.size());
	}
	public void listDataForTileset() {
		String name = getPara("name");
		String sqlExceptSelect = " FROM "+SceneLayer.TABLE_NAME+" WHERE sceneId=? and a.id=thingId )>0 THEN TRUE ELSE  FALSE END ) AS checked  from "+Model.TABLE_NAME+" a where a.classifyId=? and a.type='1' ";
		if(!StrKit.isBlank(name)) {
			sqlExceptSelect += " and a.name like '%"+name+"%' ";
		}
		List<Model> list = Model.dao.find("select a.*,(CASE  WHEN (SELECT COUNT(ID) AS COUNT "+sqlExceptSelect,this.getPara("sceneId"),this.getPara("classifyId"));
		renderPage(0, list, "", list.size());
	}
	
	public void addFor3dtitle() {
		String sceneId=this.getPara("sceneId");
		String thingId=this.getPara("id");
		Model model = Model.dao.findById(getPara("id"));
		if(model!=null) {
			SceneLayer sceneLayer=SceneLayer.dao.findFirst("select * from "+SceneLayer.TABLE_NAME+" where sceneId=? and thingId=? ",sceneId,thingId);
			if(sceneLayer==null) {
				sceneLayer=new SceneLayer();
				sceneLayer.setId(UuidUtil.getUUID());
				sceneLayer.setName(model.getName());
				sceneLayer.setCode("");
				sceneLayer.setType("0");
				sceneLayer.setSceneId(sceneId);
				sceneLayer.setParentId(sceneId);
				sceneLayer.setThingId(thingId);
				if(sceneLayer.save()) {
					model.setId(sceneLayer.getId());
					this.renderSuccess("成功！", model);
				}else {
					this.renderError("添加失败！");
				}
			}else {
				model.setId(sceneLayer.getId());
				this.renderSuccess("成功！", model);
			}
		}else {
			this.renderError("数据不存在！");
		}
	}
	
	public void save() {
		SceneModel model = getModel(SceneModel.class);
		if (SceneModel.dao.findById(model.getId())!=null) {
			if (model.update()) {
				SceneLayer sceneLayer=SceneLayer.dao.findById(model.getId());
				if(sceneLayer!=null) {
					sceneLayer.setId(model.getId());
					if(!StrKit.isBlank(model.getName())) sceneLayer.setName(model.getName());
					if(!StrKit.isBlank(model.getCode())) sceneLayer.setCode(model.getCode());
					if(!StrKit.isBlank(model.getType())) sceneLayer.setType(model.getType());
					sceneLayer.update();
				}
				renderSuccess("更新成功！",model);
			} else {
				renderError("更新失败！");
			}
		} else {
			if(StrKit.isBlank(model.getId())) model.setId(UuidUtil.getUUID());
			if (model.save()) {
				SceneLayer sceneLayer=new SceneLayer();
				sceneLayer.setId(model.getId());
				sceneLayer.setName(model.getName());
				sceneLayer.setCode(model.getCode());
				sceneLayer.setType(model.getType());
				sceneLayer.setSceneId(model.getSceneId());
				sceneLayer.setParentId(model.getSceneId());
				sceneLayer.setThingId(model.getThingId());
				if(sceneLayer.save()) {
					renderSuccess("保存成功！",model);
				}else {
					renderError("保存失败！");
					try {
						model.delete();
					}catch (Exception e) {
					}
				}
			} else {
				renderError("保存失败！");
			}
		}
	}
	
	public void getBySrcId() {
		Model model = Model.dao.findById(getPara("thingId"));
		if(model!=null) {
			this.renderSuccess("成功！", model);
		}else {
			this.renderError("数据不存在！");
		}
	}
	
	public void getByLayerId() {
		Model model = Model.dao.findFirst("select m.* from "+Model.TABLE_NAME+" m,"+SceneLayer.TABLE_NAME+" l where m.id=l.thingId and l.id=?",getPara("layerId"));
		if(model!=null) {
			this.renderSuccess("成功！", model);
		}else {
			this.renderError("数据不存在！");
		}
	}
	
	public void delete() {
		String sceneId=this.getPara("sceneId");
		String thingId=this.getPara("thingId");
		SceneLayer sceneLayer=SceneLayer.dao.findFirst("select * from "+SceneLayer.TABLE_NAME+" where sceneId=? and thingId=? ",sceneId,thingId);
		if(sceneLayer!=null) {
			if(sceneLayer.delete()) {
				SceneModel.dao.deleteById(sceneLayer.getId());
				this.renderSuccess("成功！", sceneLayer);
			}else {
				this.renderError("删除失败！");
			}
		}else {
			this.renderError("数据不存在！");
		}
	}

}
